package generatorGraphics.graphics;

import grammaticalClasses.nwConsole.ShowMote;
import java.awt.Color;
import java.awt.Dimension;
import java.text.NumberFormat;
import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

public class MoteBandwidth2 extends Graph implements GraphInterface{
    
    public MoteBandwidth2(){
        
        name = "Mote Bandwidth";
        help = "Help of Mote Bandwidth";
        
        configUnitOptions();
        
        commands.add("ShowMote");
    }
    
    private void configUnitOptions(){
        
        // Adicionando as unidades
        unitList.add("General");
        unitList.add("Per parents");
        
        //Adicionando as opções
        for (int i=0; i<unitList.size(); i++){
            ArrayList<String> aux = new ArrayList<>();
            switch (unitList.get(i)) {
                case "General":
                    aux.add("[UNIT]" + unitList.get(i));
                    aux.add("No Sort");
                    aux.add("Sort by Services");
                    aux.add("Sort by Child");
                    aux.add("Sort by Free");
                    optionsList.add(aux);
                    break;
                case "Per parents":
                    aux.add("[UNIT]" + unitList.get(i));
                    aux.add("All");
                    aux.add("Connected to Gateway");
                    aux.add("No connected to Gateway");
                    optionsList.add(aux);
                    break;
            }
        }
        
    }

    @Override
    protected void createGraph(String unit) {
        
        super.createGraph(unit);
        
        int indice;
        
//        if(search == null){
//            search = new SearchCommands(commands);
//            answer = (ArrayList<Object>) search.getCommandsAnswer();
//        }
        
        switch (unit){
            case "General":
                indice = search.getAnswer(commands, "ShowMote");
                
                chartPanel = new ChartPanel(buildGraphGeneral((ArrayList<ShowMote>) answer.get(indice)));
                chartPanel.setBackground(Color.LIGHT_GRAY);
                chartPanel.setPreferredSize(new Dimension(700, 350)); 
                chartPanel.setDomainZoomable(false);
                chartPanel.setRangeZoomable(false);

                break;
            case "Per parents":
                indice = search.getAnswer(commands, "ShowMote");

                chartPanel = new ChartPanel(buildGraphParents((ArrayList<ShowMote>) answer.get(indice)));
                chartPanel.setBackground(Color.LIGHT_GRAY);
                chartPanel.setPreferredSize(new Dimension(700, 350)); 
                chartPanel.setDomainZoomable(false);
                chartPanel.setRangeZoomable(false);

                break;
                
            default:
                chartPanel = new ChartPanel(null);
                
                break;
        }
    }    
    
    private JFreeChart buildGraphGeneral(ArrayList<ShowMote> auxMote){
        
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for(int i=0; i<auxMote.size(); i++){
            
            if(!((auxMote.get(i).getMoteType()).trim().toLowerCase().equals("ap"))){
                dataset.addValue(auxMote.get(i).getBandwidth().getBandforServices(), "Services", auxMote.get(i).getIdMote());
                dataset.addValue(auxMote.get(i).getBandwidth().getBandforChild(), "Child", auxMote.get(i).getIdMote());
                dataset.addValue(auxMote.get(i).getBandwidth().getBandFree(), "Free", auxMote.get(i).getIdMote());
            }
        }

        JFreeChart chart0 = ChartFactory.createStackedBarChart(name,
                "Devices", 
                "", 
                dataset, 
                PlotOrientation.VERTICAL, 
                true, true, true);
        chart0.setBackgroundPaint(new Color(238, 238, 238));

        CategoryPlot categoryplot = (CategoryPlot) chart0.getPlot();
        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
        numberaxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
        StackedBarRenderer stackedbarrenderer = (StackedBarRenderer)categoryplot.getRenderer();
        stackedbarrenderer.setDrawBarOutline(false);
        stackedbarrenderer.setBaseItemLabelsVisible(true);
        stackedbarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        
        return chart0;
        
    }
    
    private JFreeChart buildGraphParents(ArrayList<ShowMote> auxMote2){
    
        DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
        for(int i=0; i<auxMote2.size(); i++){
            if(!((auxMote2.get(i).getMoteType()).trim().toLowerCase().equals("ap"))){
            for(int j=0; j<auxMote2.get(i).getBandwidth().getParent().size(); j++){
                dataset2.addValue(Double.valueOf(auxMote2.get(i).getBandwidth().getBandParent().get(j)), 
                    auxMote2.get(i).getBandwidth().getParent().get(j), 
                    auxMote2.get(i).getIdMote());
            }
            }
        }

        JFreeChart chart2 = ChartFactory.createBarChart(name, 
                "Devices",
                "Value",
                dataset2,
                PlotOrientation.VERTICAL,
                true,
                true,
                false);
        chart2.setBackgroundPaint(new Color(238, 238, 238));

        CategoryPlot plot2 = (CategoryPlot) chart2.getPlot();
        plot2.setBackgroundPaint(Color.white);
        plot2.setDomainGridlinePaint(Color.BLACK);
        plot2.setDomainGridlinesVisible(true);
        plot2.setRangeGridlinePaint(Color.LIGHT_GRAY);
        
        return chart2;
    
    }
    
}
